<?php
/**
 * Created by PhpStorm.
 * User: yangos
 * Date: 18-6-17
 * Time: 上午11:50
 */

namespace vola;


class Mirror
{
    static private $bind = [];
    static public $class_name = '';
    static protected $alwaysNew = false;

    public function __construct()
    {
    }

    public static function bind($name, $value = null) {

        if (__CLASS__ != static::class) {
            return self::__callStatic('bind',func_get_args());
        }

        if (is_array($name)) {
            self::$bind = array_merge(self::$bind, $name);
        } else {
            self::$bind[$name] = $value;
        }

    }

    /**
     * @param string $class
     * @param array $argument
     * @param bool $esNew
     * @return mixed|object|\ReflectionFunction
     */
    public static function create($class = '', $argument = [], $esNew = false) {
        $class = $class ? $class : static::class;

        if ($class == static::class) {
            $class = static::$class_name;
        }

        $mirrorClass = static::getMirror();

        if ($mirrorClass) {
            $class = $mirrorClass;
        } elseif (self::$bind[$class]) {
            $class = self::$bind[$class];
        }
        if (static::$alwaysNew) {
            $esNew = self::$alwaysNew;
        }

        return Container::getInstance()->make($class, $argument, $esNew);
    }

    /**
     *  返回对应class
     *  @return string
     */
    protected static function getMirror() {
    }

//    /**
//     * 调用方法
//     * @param $name
//     * @param $arguments
//     * @return mixed
//     */
//    public function __call($name, $arguments)
//    {
//        // TODO: Implement __call() method.
//        return call_user_func_array([self::create(), $name], $arguments);
//    }

    /**
     * 调用方法
     * @param $name
     * @param $arguments
     * @return mixed
     */
    public static function __callStatic($name, $arguments)
    {
        // TODO: Implement __callStatic() method.
        return call_user_func_array([self::create(), $name], $arguments);
    }
}